<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="job_title" label="职位名称"> </el-table-column>
      <el-table-column prop="username" label="用户名" width="180">
      </el-table-column>
      <el-table-column prop="realname" label="真实姓名"> </el-table-column>
      <el-table-column prop="sex" label="性别"> </el-table-column>
      <el-table-column prop="tel" label="电话"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row, $index }">
          <el-button type="primary" size="mini" @click="alter(row, $index)"
            >修改</el-button
          >
          <el-popconfirm title="确认删除">
            <el-button
              slot="reference"
              type="danger"
              size="mini"
              @click="remove(row, $index)"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="float: right; margin: 20px; padding-right: 50px">
      <el-button type="primary" @click="add()">添加新用户</el-button>
    </div>

    <el-dialog
      title="修改用户信息"
      width="600px"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
    >
      <div>
        <el-form :model="form" label-width="120px" style="width: 480px"
          ><el-form-item label="职位名称">
            <el-input
              v-model="dialogData.job_title"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户名" :label-width="formLabelWidth">
            <el-input
              v-model="dialogData.username"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="真实姓名" :label-width="formLabelWidth">
            <el-input
              v-model="dialogData.realname"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="性别" :label-width="formLabelWidth">
            <el-input v-model="dialogData.sex" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth">
            <el-input v-model="dialogData.tel" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth">
            <el-input v-model="dialogData.email" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit()">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

 <script>
import axios from "axios";
export default {
  data() {
    return {
      dialogVisible: false,
      dialogData: {},
      dialogState: "update",
      tableData: [],
    };
  },
  created() {
    this.query();
  },

  methods: {
    query() {
      let k = this;
      axios({
        method: "post",
        url: "/user/list",
        dataType: "json",
      }).then(function (rsp) {
        console.log(rsp.data);
        if (rsp.data.msg == "success") {
          k.tableData = rsp.data.data;
        } else {
          k.$message.error("查询错误：" + rsp.data.msg);
        }
      });
    },

    alter(row, index) {
      this.dialogData = row;
      this.dialogState = "alter";
      this.dialogVisible = true;
    },
    add() {
      this.dialogData = {};
      this.dialogState = "add";
      this.dialogVisible = true;
    },
    remove(row, index) {
      let k = this;
      console.log(row, index);
      axios({
        method: "post",
        url: "/user/remove",
        dataType: "json",
        data: {
          username: row.username,
        },
      }).then(function (rsp) {
        if (rsp.data.msg == "success") {
          k.$message({
            message: "删除成功！",
            type: "success",
          });
        } else {
          k.$message.error("删除失败：" + rsp.data.msg);
        }
        k.query();
      });
    },
    submit() {
      let k = this;
      if (this.dialogState == "alter") {
        axios({
          method: "post",
          url: "/user/alter",
          dataType: "json",
          data: k.dialogData,
        }).then(function (rsp) {
          if (rsp.data.msg == "success") {
            k.$message({
              message: "修改成功！",
              type: "success",
            });
          } else {
            k.$message.error("修改失败：" + rsp.data.msg);
          }
          k.query();
        });
        this.dialogVisible = false;
      } else if (this.dialogState == "add") {
        axios({
          method: "post",
          url: "/user/add",
          dataType: "json",
          data: k.dialogData,
        }).then(function (rsp) {
          if (rsp.data.msg == "success") {
            k.$message({
              message: "添加新用户成功！",
              type: "success",
            });
          } else {
            k.$message.error("添加新用户失败：" + rsp.data.msg);
          }
          k.query();
        });
        this.dialogVisible = false;
      }
    },
  },
};
</script>